package com.yonyougov.cruxtestcase.testcase.organization.agency.myMultversion;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyougov.cruxtestcase.component.CommonLogin;
import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.dto.BaseTestReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

@Component
public class MyAgencyService {
    private final ZWCDInvoke zwcdInvoke;
    private final CommonLogin commonLogin;
    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;
    //查询
    private final String queryAgencyMuitiUrl = "/crux-org/api/agency?size=1";
    //查询by id
    private final String queryByIdAgencyMuitiUrl = "/crux-org/api/agency";
    //编辑修改
    private final String saveAgencyMuitiUrl = "/crux-org/api/agency?fetchProperties=*,parent[id,lastModifiedVersion,code,name,type],unit[id,lastModifiedVersion,code,name,type],mofDep[id,lastModifiedVersion,code,name,type],agencyType[id,lastModifiedVersion,code,name,type],region[id,lastModifiedVersion,code,name,type]";

    //参照生成
    private final String batchreferAgencyMuitiUrl = "/crux-org/api/agency/batchrefer?fetchProperties=*,parent[id,lastModifiedVersion,code,name,type],unit[id,lastModifiedVersion,code,name,type],mofDep[id,lastModifiedVersion,code,name,type],agencyType[id,lastModifiedVersion,code,name,type],region[id,lastModifiedVersion,code,name,type]";

    public MyAgencyService(ZWCDInvoke zwcdInvoke, CommonLogin commonLogin, RestTemplate restTemplate, ObjectMapper objectMapper) {
        this.zwcdInvoke = zwcdInvoke;
        this.commonLogin = commonLogin;
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    public BaseResponseDto testcase(BaseTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                httpHeaders.add("x-function-id", "3277278240327656449");
                //调用自己的api
                ResponseEntity<Object> dto = restTemplate.exchange(baseUrl + queryAgencyMuitiUrl, HttpMethod.GET, new HttpEntity<>(httpHeaders), Object.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myAgencyDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testsavecase(AgencyTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                httpHeaders.add("x-function-id", "3277278240327656449");
                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                //调用自己的api
                ArrayList<Long> list = new ArrayList<Long>();//3326750570033732635L
                list.add(baseDto.getId());
                ResponseEntity<Object> dto = restTemplate.exchange(baseUrl + batchreferAgencyMuitiUrl, HttpMethod.POST, new HttpEntity<>(list, httpHeaders), Object.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myAgencyDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    public BaseResponseDto testupdatesavecase(AgencyTestReqDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                //set header + 调用自己的api
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Authorization", "Bearer " + token);
                httpHeaders.add("x-function-id", "3277278240327656449");
                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                //调用自己的api
                //实体
                long id = baseDto.getId();
                ResponseEntity<Object> agencyDTO = restTemplate.exchange(baseUrl + queryByIdAgencyMuitiUrl + "/" + id, HttpMethod.GET, new HttpEntity<>(httpHeaders), Object.class);

                Agency agency = null;
                try {
                    agency = objectMapper.readValue(objectMapper.writeValueAsString(agencyDTO.getBody()), Agency.class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

                agency.setName("testcase01");

                ResponseEntity<Object> dto = restTemplate.exchange(baseUrl + saveAgencyMuitiUrl, HttpMethod.POST, new HttpEntity<>(agency, httpHeaders), Object.class);
                if (dto.getStatusCode() == HttpStatus.OK && dto.hasBody()) {
                    try {
                        baseResponseDto.setData("myAgencyDto", objectMapper.readValue(objectMapper.writeValueAsString(dto.getBody()), Object.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }
}
